CS 211 Lesson 13
Function Types and Scope
Quote:
Truth does not become more true by virtue of the fact that the entire world agrees with it, nor less so even if the whole world disagrees with it. Maimonides
Lesson Objectives:
Lesson:
I. MATLAB Concepts
A. Function Overview
B. Types of functions
User-defined function definitions are stored in m-files.
A m-file can contain one or more function definitions.
When multiple functions are defined in a single m-file:
The first function is the primary function (or main program), and
All other functions are called subfunctions or nested functions.
Only primary functions can be called from the MATLAB command line or from other functions in other m-files.
Functions that may be useful to many different programs should be placed in their own unique m-file as primary functions.
Functions that are designed for unique tasks related to a single computer program are typically implemented as subfunctions or nested functions.
A function's scope defines how many other functions can use (execute) it.
To facilitate testing and debugging, a function's scope should be as small as possible. This limits the number of places you have to look for errors while debugging.
- Question: Why does MATLAB have so many different types of functions with different rules for each type?
- Answer: To manage the complexity of large software systems!
- Please refer to the sample code at the end of this section as you review the following function types.
- Primary functions
- The first function defined in a .m file.
- The file name of a .m file should always be identical to the primary function name.
- Scope: only primary functions can be called (executed) from the MATLAB command line or from other MATLAB functions or scripts.
- Subfunctions
- The 2nd, 3rd, 4th, etc. function in a .m file.
- Scope: a subfunction can be called (executed) from the primary function in the same file, or from other subfunctions or nested functions in the same file.
- Nested functions
- A function whose definition is contained within another function.
- Scope: a nested function can be called (executed) from its enclosing function or from other nested functions in the same enclosing function.
- Private functions
- A primary function that is saved into a .m file in a subfolder named "private".
- Scope: only functions in the parent folder of the "private" subfolder can call (execute) private functions.
C. MATLAB's search order to find functions
- The first function found with a matching name is executed.
- Search order:
- first, MATLAB looks for a nested function inside the current function
- then, MATLAB looks for a subfunction
- then, MATLAB looks for a private function
- then, MATLAB looks for a file with the function name in the current directory (folder)
- then, MATLAB looks for a file with the function name in any directory (folder) in its search path
- then, if all this failed, MATLAB issues an error message: "function not found"
D. function functions
- A function function has input arguments that include the name of other functions
- In some circumstances, function functions allow you to apply a specific algorithm to a large set of functions and equations
- Some common MATLAB function functions include:
- eval()
- feval()
- fplot()
- ezplot()
- Some examples may help you understand the concept. Copy and paste each statement block below into MATLAB's workspace and experiment with them.
for n = 1:12 |
x = 0:0.1:10; |
II. Good Programming Practices
A. Limit Scope
When designing a large program which consists of many functions, limit the scope of a function to the smallest practical scope. This limits the code that can modify specific variables, thus limiting the code that needs to be examined when errors occur.
B. Use the appropriate type of function
In general, create nested functions when several functions are modifying the same variable(s) and the variable(s) contain large amounts of data. (This avoids the creation of duplicate copies of large data sets -- which happens when data is passed as an argument to another function.) The major purpose of nested functions is typically to break a large program into distinct sub-tasks.
In general, create subfunctions when you need code that will be executed on different data values each time the function is executed. These functions are typically called over and over again to implement a common, repeated task.
In general, create private functions if you are re-defining a built-in function and you only want the functions in a particular folder to use the modified version.
III. Algorithms
(None for this lesson)
Lab Work: Lab 13
References: Chapman Textbook: section 5.7-5.8